<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2023/8/3
 * Time: 3:42 PM
 */

namespace App\Models\CacheModel\zlcr3;

use App\Models\Api\BaseCache;
use think\Log;
use AstroTool\Cache\CacheManage;

class Zlcr3ConsultChatRtcRecordComment extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zlcr3_consult_chat_rtc_record_comment';
    protected $cacheModel = 'voice_consult.comment';


    /**
     * 获取用户背包记录
     * @param int $astrId
     * @return array|mixed
     * @throws \Exception
     */
    public function astrCommentList(int $astrId)
    {
        $page = input('page/d', 1);
        return CacheManage::getInstance()
            ->setCacheConfig($this->cacheModel, $astrId)
            ->setPage($page)
            ->setSort('desc')
            ->getListPage(function ($ids = [], $isCache = true, $sortType = 'desc') use ($astrId)
            {
                return $this->getInfoByIdsInData($ids, [$astrId], $sortType);
            }, 'create_time', [], 0);
    }

    public function saveData($data)
    {
        try
        {
            $this->insert([
                'rtc_id' => $data['rtc_id'],
                'uid' => $data['uid'],
                'astr_id' => $data['astr_id'],
                'level' => $data['level'],
                'tid' => $data['tid'],
                'comment' => $data['comment'],
                'comment_type' => 1,
                'create_time' => time()
            ]);
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $data['astr_id'])->rmCache('list');
        }
        catch (\Exception $e)
        {
            Log::write('Zlcr3ConsultChatRtcRecordComment model => ()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return false;
        }
        return true;
    }

    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            if ($ids)
            {
                $this->alias('c')->whereIn('c.rtc_id', $ids);
            } else
            {
                $this->alias('c')->where('c.astr_id', $type[0]);
            }
            return $this->join('wechat_backup_07_18_14_43_00.app_user u', 'c.uid=u.id')->column('c.*,u.nickname name,u.avatar');
        }
        catch (\Exception $e)
        {
            Log::write('Zlcr3ConsultChatRtcRecordComment model => ()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return [];
        }
    }
}
